{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Python enumerate() built-in-function.ipynb",
      "provenance": [],
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/Tanu-N-Prabhu/Python/blob/master/Python_enumerate()_built_in_function.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "SlS3uIP0UrJU",
        "colab_type": "text"
      },
      "source": [
        "# Python enumerate() built-in-function\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "iA6kOMpEUt37",
        "colab_type": "text"
      },
      "source": [
        "## In this tutorial let us understand the enumerate built-in-function in python.\n",
        "\n",
        "![alt text](https://miro.medium.com/max/1523/1*RXF13Ul8pCJuCsjlE_KNww.png)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "2bbG4M3nU15D",
        "colab_type": "text"
      },
      "source": [
        "# Definition\n",
        "\n",
        "An enumerator built-in-function adds a counter of iterable numbers to the provided data structure of integers, characters or strings and many more. Now the data structure might be any list, tuple, dictionary or sets. If the counter is not provided by the user, then it starts from 0 by default. Based on the number provided the enumerator function iterates.\n",
        "\n",
        "\n",
        "\n",
        "---\n",
        "\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "plXrOTBIU8Cc",
        "colab_type": "text"
      },
      "source": [
        "# Syntax\n",
        "\n",
        "**The syntax of the enumerator built-in-function is as shown below:**\n",
        "\n",
        "\n",
        "\n",
        "```\n",
        "enumerate(iterable, start)\n",
        "```\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "P5iazrUeYaFE",
        "colab_type": "text"
      },
      "source": [
        "\n",
        "\n",
        "---\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "CDjEenabVGaS",
        "colab_type": "text"
      },
      "source": [
        "# Parameters\n",
        "\n",
        "**The parameters of the enumerator function are iterable and start.**\n",
        "\n",
        "1. iterable: This is the name of the data structure that wish to iterate.\n",
        "2. start: This is the number where you want to start iterating the counter.\n",
        "\n",
        "\n",
        "\n",
        "> ***Note: The iterable must be an object that supports iteration.***\n",
        "\n",
        "If you are curious to know the actual syntax of the enumerate function just type help(enumerate) on your IDE and then you will get the following result. Because you need to know this stuff.\n",
        "\n",
        "\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "QfDTthUzVYbu",
        "colab_type": "code",
        "outputId": "85b4ab0a-b5de-4f8d-bb31-001ea6cc1c96",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 503
        }
      },
      "source": [
        "help(enumerate)"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Help on class enumerate in module builtins:\n",
            "\n",
            "class enumerate(object)\n",
            " |  enumerate(iterable[, start]) -> iterator for index, value of iterable\n",
            " |  \n",
            " |  Return an enumerate object.  iterable must be another object that supports\n",
            " |  iteration.  The enumerate object yields pairs containing a count (from\n",
            " |  start, which defaults to zero) and a value yielded by the iterable argument.\n",
            " |  enumerate is useful for obtaining an indexed list:\n",
            " |      (0, seq[0]), (1, seq[1]), (2, seq[2]), ...\n",
            " |  \n",
            " |  Methods defined here:\n",
            " |  \n",
            " |  __getattribute__(self, name, /)\n",
            " |      Return getattr(self, name).\n",
            " |  \n",
            " |  __iter__(self, /)\n",
            " |      Implement iter(self).\n",
            " |  \n",
            " |  __new__(*args, **kwargs) from builtins.type\n",
            " |      Create and return a new object.  See help(type) for accurate signature.\n",
            " |  \n",
            " |  __next__(self, /)\n",
            " |      Implement next(self).\n",
            " |  \n",
            " |  __reduce__(...)\n",
            " |      Return state information for pickling.\n",
            "\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "jalrJE4lYXP6",
        "colab_type": "text"
      },
      "source": [
        "\n",
        "\n",
        "---\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "tPCnslpIVjuB",
        "colab_type": "text"
      },
      "source": [
        "# Return Type\n",
        "\n",
        "The return type of an enumerate function is an object type. So the enumerate function returns an object by adding the iterating counter value to it. You can also convert the enumerator object into a list(), tuple(), set() and many more."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "i75iLR34Vn68",
        "colab_type": "text"
      },
      "source": [
        "**Let us see an example and understand the enumerate function.**\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "251JQsEzVqF5",
        "colab_type": "code",
        "outputId": "7cb52601-e8f2-4198-c630-01056acac5fa",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 52
        }
      },
      "source": [
        "programmming = [\"Python\", \"Programmming\", \"Is\", \"Fun\"]\n",
        "print(type(programmming))\n",
        "enum = enumerate(programmming)\n",
        "print(type(enum))"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "<class 'list'>\n",
            "<class 'enumerate'>\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "i7hqaGlZVx81",
        "colab_type": "code",
        "outputId": "5b336ca9-1bea-4346-b656-001ea427d390",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        }
      },
      "source": [
        "#Converting to a list\n",
        "print(list(enum))"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "[(0, 'Python'), (1, 'Programmming'), (2, 'Is'), (3, 'Fun')]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "O0N9vLHzV3ZB",
        "colab_type": "code",
        "outputId": "16d7c435-9c7e-40fe-fe62-0e97c024444f",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        }
      },
      "source": [
        "#Converting to a tuple\n",
        "print(tuple(enum))"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "((0, 'Python'), (1, 'Programmming'), (2, 'Is'), (3, 'Fun'))\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Ss6Sq8ApV8Ok",
        "colab_type": "code",
        "outputId": "6c69cc07-c66b-4251-c355-ed8e6bc6d4ee",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        }
      },
      "source": [
        "#Converting to a set\n",
        "print(set(enum))"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "{(3, 'Fun'), (0, 'Python'), (2, 'Is'), (1, 'Programmming')}\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "9o-1wMIlWDai",
        "colab_type": "text"
      },
      "source": [
        "When you run the program you will get the following output:\n",
        "\n",
        "\n",
        "\n",
        "```\n",
        "<class 'list'>\n",
        "<class 'enumerate'>\n",
        "[(0, 'Python'), (1, 'Programmming'), (2, 'Is'), (3, 'Fun')]\n",
        "((0, 'Python'), (1, 'Programmming'), (2, 'Is'), (3, 'Fun'))\n",
        "{(3, 'Fun'), (2, 'Is'), (0, 'Python'), (1, 'Programmming')}\n",
        "```\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "lKqT4kF6WNQG",
        "colab_type": "text"
      },
      "source": [
        "\n",
        "\n",
        "---\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "-zhFApMjWO75",
        "colab_type": "text"
      },
      "source": [
        "# Optional Arguments\n",
        "\n",
        "Enumerate function also accepts optional arguments, you can also pass the name of the data structure along with the specific index which you want to start the counter from. For example, think that the default value of the list starts from 0 and then the counter continues to iterate. Now you want to start the counter from 5 and then increment it. This can be done as shown below:"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "RSMWM1SXWUjr",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "programmming = [\"Python\", \"Programmming\", \"Is\", \"Fun\"]"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "BsdfMet_WWhP",
        "colab_type": "code",
        "outputId": "3aeff072-c6c9-40ed-ceb2-db17c9d4b0aa",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        }
      },
      "source": [
        "# Counter value starts from 0\n",
        "enum = enumerate(programmming)\n",
        "print(list(enum))"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "[(0, 'Python'), (1, 'Programmming'), (2, 'Is'), (3, 'Fun')]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ouxYi2N7WYIn",
        "colab_type": "code",
        "outputId": "e31fc421-2b7e-4603-b419-48db0308046e",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        }
      },
      "source": [
        "# Counter value starts from 5\n",
        "enum = enumerate(programmming, 5)\n",
        "print(list(enum))"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "[(5, 'Python'), (6, 'Programmming'), (7, 'Is'), (8, 'Fun')]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "hQ22h7RMWaA7",
        "colab_type": "text"
      },
      "source": [
        "When you run the program you will get the following output:\n",
        "\n",
        "\n",
        "\n",
        "```\n",
        "[(0, 'Python'), (1, 'Programmming'), (2, 'Is'), (3, 'Fun')]\n",
        "[(5, 'Python'), (6, 'Programmming'), (7, 'Is'), (8, 'Fun')]\n",
        "```\n",
        "\n",
        "\n",
        "\n",
        "\n",
        "---\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "1-FtDcXFXm2Z",
        "colab_type": "text"
      },
      "source": [
        "# Looping Over an Enumerate object\n",
        "\n",
        "Let us see how can we loop over an enumerate as shown below:\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "FHvB3KhlXsPG",
        "colab_type": "code",
        "outputId": "9003e23e-2b66-4c24-a47d-b73e2dc203e4",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 156
        }
      },
      "source": [
        "programmming = [\"Python\", \"Programmming\", \"Is\", \"Fun\"]\n",
        "for i in enumerate(programmming):\n",
        "    print(i)\n",
        "for i in enumerate(programmming, 10):\n",
        "    print(i)"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "(0, 'Python')\n",
            "(1, 'Programmming')\n",
            "(2, 'Is')\n",
            "(3, 'Fun')\n",
            "(10, 'Python')\n",
            "(11, 'Programmming')\n",
            "(12, 'Is')\n",
            "(13, 'Fun')\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "1tXkwc9iXudb",
        "colab_type": "text"
      },
      "source": [
        "When you run the program the output will be:\n",
        "\n",
        "\n",
        "\n",
        "```\n",
        "(0, 'Python') \n",
        "(1, 'Programmming') \n",
        "(2, 'Is') \n",
        "(3, 'Fun')\n",
        "\n",
        "(10, 'Python') \n",
        "(11, 'Programmming') \n",
        "(12, 'Is') \n",
        "(13, 'Fun')\n",
        "```\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "WHwhdDgjX1in",
        "colab_type": "text"
      },
      "source": [
        "This is the end of the tutorial about ***“Python enumerate() built-in-function”***, this is a very short tutorial because this concept is very small and it is not much you can do with it. It is really important to know the concept of Enumerate function so that you can apply this concept elsewhere. To read the official documentation of the enumerate function please go through the link below:\n",
        "\n",
        "[Python Enumerate Function](https://docs.python.org/3/library/functions.html?source=post_page-----acccf988d096----------------------#enumerate)\n",
        "\n",
        "\n",
        "Alright time to say Goodbye, Stay tuned to read more python tutorials. I hope you enjoyed reading this tutorial. Have a good day!\n",
        "\n",
        "\n",
        "\n",
        "---\n",
        "\n",
        "\n",
        "# Author: Tanu Nanda Prabhu"
      ]
    }
  ]
}